package cn.iocoder.springboot.lab72;

import org.junit.Test;
import org.postgresql.jdbc.PgResultSetMetaData;

import java.sql.*;

public class ResultSetMetadataDemo {


    @Test
    public void test1() throws SQLException {
        Connection connect = DriverManager.getConnection("jdbc:postgresql://172.16.55.184:5432/vastbase", "zxs", "Aa123456");
        // 别名加上双引号
        String sql = "select col1, col2 as acol2 from t1";
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = resultSetMetaData.getColumnName(i);
            System.out.println(columnName);
        }
    }


    @Test
    public void test2() throws SQLException {
        // ?db_compatibility=oracle&resultCaseMode=upper
        Connection connect = DriverManager.getConnection("jdbc:postgresql://172.16.55.176:5432/vastbase", "zxs", "Vastdata#0730");
        String sql = "select col1, col2 as acol2  from t1";
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = resultSetMetaData.getColumnName(i);
            // 获取原来的列名
            String baseColumnName = ((PgResultSetMetaData) resultSetMetaData).getBaseColumnName(1);
            System.out.println("列名：" + columnName);
            System.out.println("原列名：" + baseColumnName);
            if (baseColumnName.equals(columnName)) {
                System.out.println("修改后的列名：" + columnName.toUpperCase());
            }
        }
    }


    @Test
    public void test3() throws SQLException {
        // ?db_compatibility=oracle&resultCaseMode=upper
        Connection connect = DriverManager.getConnection("jdbc:postgresql://172.16.55.184:5432/vastbase", "zxs", "Aa123456");
        String sql = "select col1 as \"COL1\", col2 as acol2  from t1";
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = resultSetMetaData.getColumnName(i);
            // 获取原来的列名
            String baseColumnName = ((PgResultSetMetaData) resultSetMetaData).getBaseColumnName(1);
            boolean caseSensitive = resultSetMetaData.isCaseSensitive(i);
            System.out.println("列名：" + columnName);
            System.out.println("原列名：" + baseColumnName);
            System.out.println("大小写敏感：" + caseSensitive);
        }
    }

    @Test
    public void test4() throws SQLException {
        Connection connect = DriverManager.getConnection("jdbc:postgresql://172.16.55.176:5432/vastbase?resultCaseMode=upper", "zxs", "Vastdata#0730");
        String sql = "select col1, col2 as \"acol2\" from t1";
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = resultSetMetaData.getColumnName(i);
            String baseColumnName = ((PgResultSetMetaData) resultSetMetaData).getBaseColumnName(1);
            System.out.println("列名：" + columnName);
            System.out.println("原列名：" + baseColumnName);
        }
    }
}
